﻿using System.Collections.ObjectModel;
using System.Data;
using System.IO;
using System.Runtime.Serialization;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using PublicDomain.Data.Csv.Metadata;

namespace PublicDomain.UnitTest.Data.Csv.Metadata
{
    [TestClass]
    public class CsvSchemataTest
    {
        public TestContext TestContext { get; set; }

        public void TestInitialize()
        {
        }

        [TestMethod]
        [Description("Tests CsvSchemata serialization.")]
        public void CsvSchemataSerializationTest()
        {
            var target = new CsvSchemata();

            target.CsvDataFormats = new Collection<CsvDataFormat>
            {
                new CsvDataFormat
                {
                    Name = "Schema1",
                    CsvColumnsDefinitions = new Collection<CsvColumnDefinition> 
                    {
                        new CsvColumnDefinition { ColumnName = "Column01", ColumnOrdinal = 1, DataType = DbType.String, ColumnSize = 254 },
                        new CsvColumnDefinition { ColumnName = "Column02", ColumnOrdinal = 2, DataType = DbType.Double }
                    }
                },
                new CsvDataFormat
                {
                    Name = "Schema2",
                    CsvColumnsDefinitions = new Collection<CsvColumnDefinition> 
                    {
                        new CsvColumnDefinition { ColumnName = "ColumnA", ColumnOrdinal = 1, DataType = DbType.Int32 },
                        new CsvColumnDefinition { ColumnName = "ColumnB", ColumnOrdinal = 2, DataType = DbType.String },
                        new CsvColumnDefinition { ColumnName = "ColumnC", ColumnOrdinal = 3, DataType = DbType.DateTime }
                    }
                }
            };

            target.CsvFilesFormats = new Collection<CsvFileFormat>
            {
                new CsvFileFormat { FileName = "File01.csv", CsvDataFormatName = "Schema1" },
                new CsvFileFormat { FileName = "File02.csv", CsvDataFormatName = "Schema1" },
                new CsvFileFormat { FileName = "File03.csv", CsvDataFormatName = "Schema2" }
            };

            var dataContractSerializer = new DataContractSerializer(typeof(CsvSchemata));
            using (var fileStream = new FileStream("CsvSchemata.xml", FileMode.Create))
            {
                dataContractSerializer.WriteObject(fileStream, target);
            }
        }
    }
}
